package mapReduce.homework.q1q2_generateDate;

import mapReduce.homework.util.CommonVar;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class GenerateData {
    //当前工程根目录
    String path = CommonVar.PATH;
    //声明全局随机数对象
    Random random = new Random();
    //年份map，控制每年订单编号的有序性
    Map<Integer,Integer> yearMap = new HashMap<Integer, Integer>();
    //套餐map，控制每个套餐编号的有序性
    Map<String,Integer> menuMap = new HashMap<String, Integer>();
    //初始化合作方数组
    String[] partners = {"艺龙网","携程网","去哪网","58旅游"};
    //初始化合作方代号数组
    String[] partnerNums = {"YL","XC","QN","WB"};

    /**
     * 生成订单数据
     */
    public void genOrderData() throws IOException {
        FileWriter fw = new FileWriter(path+"order.txt");
        //生成10万条数据
        for(int i=0;i<100000;i++){
            //年份是2000-2022年随机数
            int year = random.nextInt(23)+2000;
            //初始化orderId
            String orderId = "";
            //如果map中存在当前年份，则读出原计数加1显示，并把加1之后的数据更新到map中
            //通过String.format("%nd",m).replace(" ", "0");补全位数
            if(yearMap.containsKey(year)){
                orderId = "DD"+year+getFullNum(5,yearMap.get(year)+1);
                yearMap.put(year,yearMap.get(year)+1);
            }else{
                //如果map中不存在当前年份，则计数显示为1，并把数据更新到map中
                orderId = "DD"+year+getFullNum(5,1);
                yearMap.put(year,1);
            }

            //获得一个合作方的随机数在0-（数组长度-1）范围内
            int randomPartner = random.nextInt(partners.length);
            //获得合作方名称
            String partner = partners[randomPartner];
            //获得合作方相应的套餐名
            String partnerNum = partnerNums[randomPartner]+"Y"+getFullNum(2,random.nextInt(99)+1);

            fw.write(year+"\t"+orderId+"\t"+partner+"\t"+partnerNum+"\n");
        }
        fw.flush();
        fw.close();
    }

    public void genMenuSeqData() throws IOException {
        //创建sequence文件所需对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path outputPath = new Path(path+"menu.seq");
        SequenceFile.Writer out = SequenceFile.createWriter(fs,conf,outputPath, Text.class,Text.class);

        Text outKey = new Text();
        Text outValue = new Text();

        //遍历所有的合作方
        for(String partnerNum:partnerNums){
            //每个合作方都有1-99个套餐，格式为 WBY01  1234.00
            for(int i=1;i<100;i++){
                //套餐编号
                String menuName = partnerNum+"Y"+getFullNum(2,i);
                //随机出价格，后保留.00格式
                DecimalFormat df = new DecimalFormat("#.00");
                String money = df.format(random.nextInt(49901)+100);

                outKey.set(menuName);
                outValue.set(money);
//                System.out.println("name:"+menuName);
//                System.out.println("money:"+money);
                out.append(outKey,outValue);
            }
        }
        out.hflush();
        out.close();
    }

    public String getFullNum(int count,int num){
        return String.format("%"+count+"d",num).replace(" ","0");
    }

    public static void main(String[] args) throws IOException {
        new GenerateData().genOrderData();
//        new GenerateData().genMenuSeqData();
    }

}
